package edu.cmu.cs.webapp.ioms.ui.wizards.ingredient;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.rwt.RWT;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.omg.CORBA._PolicyStub;

import edu.cmu.cs.webapp.ioms.dao.DataAccessException;
import edu.cmu.cs.webapp.ioms.dao.impl.IngredientDAOImpl;
import edu.cmu.cs.webapp.ioms.models.Ingredient;
import edu.cmu.cs.webapp.ioms.models.User;
import edu.cmu.cs.webapp.ioms.utils.SWTUtils;

public class IngreidentMainWizard extends WizardPage {

	private Text _ingName;
	// need to decide later whether to include another pop-up box for this
	// functionality
	private Text _ingInventory;
	private Text _ingUnitCost;
	private Text _remark;

	private Ingredient _model;

	protected IngreidentMainWizard(String pageName) {
		super(pageName);
		setTitle("Ingredient Main");
		setDescription("Please input the information for the ingredient");
	}

	public IngreidentMainWizard(String pageName, String title,
			ImageDescriptor titleImage) {
		super(pageName, title, titleImage);
	}

	@Override
	public void createControl(Composite parent) {
		// direct container under parent
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;

		container.setLayout(layout);
		
		// layout.verticalSpacing = 9;

		// set the first column in the contianer, usually is the name of the
		// input
		SWTUtils.createLabel(container, "Ingredient Name:", 1);
		
		_ingName = new Text(container, SWT.BORDER);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 1;
		_ingName.setLayoutData(gd);
		_ingName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});
		this._ingName.setEditable(true);

		// create a line between the product name and other element, deliminator
		createLine(container, 2);

		SWTUtils.createLabel(container, "Inventory:", 1);
		_ingInventory = new Text(container, SWT.BORDER);
		_ingInventory.setLayoutData(gd);
		_ingInventory.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});
		this._ingInventory.setEditable(true);

		SWTUtils.createLabel(container, "Unit cost:", 1);
		_ingUnitCost = new Text(container, SWT.BORDER);
		_ingUnitCost.setLayoutData(gd);
		_ingUnitCost.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		createLine(container, 2);

		SWTUtils.createLabel(container, "Remark:", 1);
		_remark = new Text(container, SWT.BORDER);
		_remark.setLayoutData(gd);
		_remark.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				validate();
			}
		});

		if (isUpdate()) {
			loadFromExistingModel();
		}
		setControl(container);
		validate();
	}

	private void loadFromExistingModel() {
		// put the selected information into the pop-ups
		_model = getModel();

		// if current model has been chosen and is not null;
		if (_model != null) {
			_ingName.setText(_model.getName());
			_ingInventory.setText(String.valueOf(_model.getInventory()));
			_ingUnitCost.setText(""+_model.getUnit_cost());
			// _price.setText(model.getProd_description());
			_remark.setText(_model.getRemark());
		}
	}

	private Ingredient getModel() {
		return ((IngredientWizard) getWizard()).getModel();
	}

	private void createLine(Composite container, int numColumns) {
		Label line = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = numColumns;
		line.setLayoutData(gridData);

	}

	private boolean validate() {
		
		boolean valid = true;
		StringBuffer errorMessage = new StringBuffer();
		List<String> names = new ArrayList<String>(); ;
		try {
			List<Ingredient> ingredients = new IngredientDAOImpl().getAllIngr();
			
			for (Ingredient i : ingredients){
				names.add(i.getName());
			}
			
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		if (_ingName == null || _ingName.getText().length() == 0) {
			errorMessage.append("You must enter a ingredient name");
			valid= false;
		}
		
		if (names.contains(_ingName.getText())){
			errorMessage.append("There is already an ingredient of same name, sitll want to continue?");
			
		}
		try {
			Double unit_cost = Double.parseDouble(_ingUnitCost.getText().trim());
			Double inventory = Double.parseDouble(_ingInventory.getText().trim());
		} catch (NumberFormatException ex) {
			errorMessage.append("Please input valid number for unit cost and inventory.");
			valid =false;
		}
		
		
		if (valid){
			setErrorMessage(null);
			setPageComplete(true);
		}
		else {
			setErrorMessage(errorMessage.toString());
			setPageComplete(false);
		}
		return valid;
	}

	private boolean isUpdate() {
		return ((IngredientWizard) getWizard()).isUpdate();
	}

	public String get_ingName() {
		return _ingName.getText();
	}

	public double get_ingInventory() {
		return Double.parseDouble(_ingInventory.getText().trim());
	}

	public double get_ingUnitCost() {
		return Double.parseDouble(_ingUnitCost.getText().trim());
	}

	public String get_remark() {
		return _remark.getText();
	}

}
